{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1输入某年某月某日，判断这一天是这一年的第几天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def day_of_year_question():\n",
    "    def is_leap_year(year):\n",
    "        \"\"\"判断是否为闰年\"\"\"\n",
    "        return (year % 4 == 0 and year % 100!= 0) or year % 400 == 0\n",
    "\n",
    "    year = int(input(\"请输入年份：\"))\n",
    "    month = int(input(\"请输入月份：\"))\n",
    "    day = int(input(\"请输入日期：\"))\n",
    "    days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    if is_leap_year(year):\n",
    "        days_in_month[2] = 29\n",
    "    total_days = 0\n",
    "    for m in range(1, month):\n",
    "        total_days += days_in_month[m]\n",
    "    total_days += day\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{total_days}天。\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2判断 1000 中有多少个素数，并输出所有素数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def prime_numbers_question():\n",
    "    prime_numbers = []\n",
    "    for num in range(2, 1001):\n",
    "        is_prime = True\n",
    "        for i in range(2, int(num ** 0.5) + 1):\n",
    "            if num % i == 0:\n",
    "                is_prime = False\n",
    "                break\n",
    "        if is_prime:\n",
    "            prime_numbers.append(num)\n",
    "    print(f\"1000以内的素数共有{len(prime_numbers)}个，分别为：\")\n",
    "    print(prime_numbers)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3利用条件运算符的嵌套来完成此题：学习成绩 >=90 分的同学用 A 表示，60-89 分之间的用 B 表示，60 分以下的用 C 表示。（要求有键盘输入）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "def grade_question():\n",
    "    score = float(input(\"请输入学习成绩：\"))\n",
    "    grade = 'A' if score >= 90 else ('B' if score >= 60 else 'C')\n",
    "    print(f\"该成绩对应的等级为：{grade}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def string_count_question():\n",
    "    input_str = input(\"请输入一行字符串：\")\n",
    "    letters_count = 0\n",
    "    spaces_count = 0\n",
    "    digits_count = 0\n",
    "    others_count = 0\n",
    "    for char in input_str:\n",
    "        if char.isalpha():\n",
    "            letters_count += 1\n",
    "        elif char.isspace():\n",
    "            spaces_count += 1\n",
    "        elif char.isdigit():\n",
    "            digits_count += 1\n",
    "        else:\n",
    "            others_count += 1\n",
    "    print(f\"英文字母个数：{letters_count}\")\n",
    "    print(f\"空格个数：{spaces_count}\")\n",
    "    print(f\"数字个数：{digits_count}\")\n",
    "    print(f\"其它字符个数：{others_count}\")\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5一个数如果恰好等于它的因子之和，这个数就称为 “完数”。例如 6=1＋2＋3. 编程，找出 1000 以内的所有完数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "def perfect_numbers_question():\n",
    "    for num in range(1, 1001):\n",
    "        factors_sum = 0\n",
    "        for factor in range(1, num):\n",
    "            if num % factor == 0:\n",
    "                factors_sum += factor\n",
    "        if factors_sum == num:\n",
    "            print(num)\n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6输入任意数 N，求 1+2!+3!+...+N! 的值（注意实数范围）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def factorial_sum_question():\n",
    "    n = int(input(\"请输入数N：\"))\n",
    "    sum_value = 0\n",
    "    factorial = 1\n",
    "    for i in range(1, n + 1):\n",
    "        factorial *= i\n",
    "        sum_value += factorial\n",
    "    print(f\"1+2!+3!+...+{n}! 的值为：{sum_value}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7输入任意奇数 N，按如下要求打印出图案（菱形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "def diamond_question():\n",
    "    n = int(input(\"请输入奇数N：\"))\n",
    "    for i in range(1, (n + 1) // 2 + 1):\n",
    "        print(' ' * ((n + 1) // 2 - i) + '*' * (2 * i - 1))\n",
    "    for i in range((n + 1) // 2 - 1, 0, -1):\n",
    "        print(' ' * ((n + 1) // 2 - i) + '*' * (2 * i - 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8对 10 个数进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sort_numbers_question():\n",
    "    numbers = []\n",
    "    for _ in range(10):\n",
    "        num = int(input(\"请输入一个数：\"))\n",
    "        numbers.append(num)\n",
    "    numbers.sort()\n",
    "    print(\"排序后的结果为：\", numbers)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9有一个已经排好序的数组（元素数量大于 10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "def insert_number_question():\n",
    "    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]  # 这里只是示例已有排序数组，你可替换或修改获取方式\n",
    "    num_to_insert = int(input(\"请输入要插入的数：\"))\n",
    "    inserted = False\n",
    "    for i in range(len(sorted_array)):\n",
    "        if num_to_insert < sorted_array[i]:\n",
    "            sorted_array.insert(i, num_to_insert)\n",
    "            inserted = True\n",
    "            break\n",
    "    if not inserted:\n",
    "        sorted_array.append(num_to_insert)\n",
    "    print(\"插入后的数组为：\", sorted_array)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10有 n 个人围成一圈，顺序排号。从第一个人开始报数（从 1 到 3 报数），凡报到 3 的人退出圈子，问最后留下的是原来第几号的那位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "def circle_question():\n",
    "    n = int(input(\"请输入人数n：\"))\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    print(f\"最后留下的是原来第{people[0]}号的那位。\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "选择题目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2!+3!+...+1! 的值为：1\n"
     ]
    }
   ],
   "source": [
    "choice = input(\"请输入题目的序号（1 - 10）来运行相应程序: \")\n",
    "if choice == \"1\":\n",
    "    day_of_year_question()\n",
    "elif choice == \"2\":\n",
    "    prime_numbers_question()  # 添加对应的函数调用，并且按照Python语法规范缩进\n",
    "elif choice == \"3\":\n",
    "    grade_question()\n",
    "elif choice == \"4\":\n",
    "    string_count_question()\n",
    "elif choice == \"5\":\n",
    "    perfect_numbers_question()\n",
    "elif choice == \"6\":\n",
    "    factorial_sum_question()\n",
    "elif choice == \"7\":\n",
    "    diamond_question()\n",
    "elif choice == \"8\":\n",
    "    sort_numbers_question()\n",
    "elif choice == \"9\":\n",
    "    insert_number_question()\n",
    "elif choice == \"10\":\n",
    "    circle_question()\n",
    "else:\n",
    "    print(\"输入的序号不合法，请输入1 - 10来选择对应的题目程序。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
